/*
 * DirTile.cpp
 *
 *  Created on: Jan 4, 2009
 *      Author: Carmine Red
 */

#include "DirTile.h"

DirTile::DirTile()
{
	dir = NODIRECTION;
	isEntrance = isSet = isMousedOver = false;

	//load texture
	HR(D3DXCreateTextureFromFile(gd3dDevice, "Textures//DTileTest.bmp", &m_DTSprite));
	HR(D3DXCreateTextureFromFile(gd3dDevice, "Textures//DTileEmptyTest.bmp", &m_DTESprite));
	HR(D3DXCreateTextureFromFile(gd3dDevice, "Textures//DTilePreviewTest.bmp", &m_DTPSprite));
	HR(D3DXCreateTextureFromFile(gd3dDevice, "Textures//DTileEmptyPreviewTest.bmp", &m_DTEPSprite));

	m_DTPos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
}

DirTile::~DirTile()
{
	ReleaseCOM(m_DTSprite);
	ReleaseCOM(m_DTESprite);
	ReleaseCOM(m_DTPSprite);
	ReleaseCOM(m_DTEPSprite);
}

void DirTile::render(Prismatic* game)
{
	IDirect3DTexture9* texToUse;

	if(isMousedOver)
	{
		// if this tile is moused over
		if(dir != NODIRECTION)
		{
			// if it has a direction
			texToUse = m_DTPSprite;
		}
		else
		{
			// if it does not have a direction
			texToUse = m_DTEPSprite;
		}
	}
	else
	{
		// this tile is not moused over
		if(isSet && dir != NODIRECTION)
		{
			// the tile is set and has a direction
			texToUse = m_DTSprite;
		}
		else
		{
			// else it's just not active
			texToUse = m_DTESprite;
		}
	}

	// set rotation

	float rotationZ;

	switch(dir)
	{
	case UP:
		rotationZ = 0.0;
		break;
	case RIGHT:
		rotationZ = 270.0;
		break;
	case DOWN:
		rotationZ = 180.0;
		break;
	case LEFT:
		rotationZ = 90.0;
		break;
	default:
		rotationZ = 0.0;
	}

	// draaaaaaaaaaawwww

	D3DXMATRIX matRotateZ, matTrans;

	D3DXMatrixRotationZ(&matRotateZ, (rotationZ * M_PI / 180.0));

	D3DXMatrixTranslation(&matTrans, m_DTPos.x, m_DTPos.y, m_DTPos.z);

	//set flags for this render and begin
	HR(game->Sprite()->Begin(D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE));

	//for some reason our x and y values are flipped
	// WHHHHHHHHHHHYYYYYYYYYYYYYYYYYYYYY

	D3DXVECTOR3 debugPosData(-m_DTPos.x, -m_DTPos.y, m_DTPos.z);

	// account for center of sprite

	D3DXVECTOR3 center(DIRTILESIDE * 0.5, DIRTILESIDE * 0.5, 0.0);

	debugPosData.x += DIRTILESIDE * 0.5;
	debugPosData.y += DIRTILESIDE * 0.5;

	// set rotation

	HR(game->Sprite()->SetTransform(&(matRotateZ * matTrans)));

	//draw background, plain and simple, not doing any translations or anything
	//but if i was, they would go here as well
	HR(game->Sprite()->Draw(texToUse, 0, &center, 0, D3DCOLOR_XRGB(255, 255, 255)));
	HR(game->Sprite()->Flush());

	//be sure to end when done
	HR(game->Sprite()->End());

	/*
	if(dir != NODIRECTION)
	{
		if(true)
		{
			// draaaaaaaaaaawwww

			//set flags for this render and begin
			HR(game->Sprite()->Begin(D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE));

			//for some reason our x and y values are flipped
			// WHHHHHHHHHHHYYYYYYYYYYYYYYYYYYYYY

			D3DXVECTOR3 debugPosData(-m_DTPos.x, -m_DTPos.y, m_DTPos.z);

			// account for center of sprite
			debugPosData.x += DIRTILESIDE * 0.5;
			debugPosData.y += DIRTILESIDE * 0.5;

			//draw background, plain and simple, not doing any translations or anything
			//but if i was, they would go here as well
			HR(game->Sprite()->Draw(m_DTSprite, 0, &debugPosData, 0, D3DCOLOR_XRGB(255, 255, 255)));
			HR(game->Sprite()->Flush());

			//be sure to end when done
			HR(game->Sprite()->End());

			// if isEntrance == true, use diff texture
		}
	}
	else
	{
		if(true)
		{
			// draaaaaaaaaaawwww

			//set flags for this render and begin
			HR(game->Sprite()->Begin(D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE));

			//for some reason our x and y values are flipped
			// WHHHHHHHHHHHYYYYYYYYYYYYYYYYYYYYY

			D3DXVECTOR3 debugPosData(-m_DTPos.x, -m_DTPos.y, m_DTPos.z);

			// account for center of sprite
			debugPosData.x += DIRTILESIDE * 0.5;
			debugPosData.y += DIRTILESIDE * 0.5;

			//draw background, plain and simple, not doing any translations or anything
			//but if i was, they would go here as well
			HR(game->Sprite()->Draw(m_DTSprite, 0, &debugPosData, 0, D3DCOLOR_XRGB(255, 255, 255)));
			HR(game->Sprite()->Flush());

			//be sure to end when done
			HR(game->Sprite()->End());

			// if isEntrance == true, use diff texture
		}
	}
	// will only draw if active and has a direction
	 */
}

void DirTile::renderPreview(Prismatic* game)	// for previewed DirTile placement
{
	// should only be called explicitly: only one tile is preview at a time

	// draaaaaaaaaaawwww with darkened/transparent value

	// draaaaaaaaaaawwww

	//set flags for this render and begin
	HR(game->Sprite()->Begin(D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE));

	//for some reason our x and y values are flipped
	// WHHHHHHHHHHHYYYYYYYYYYYYYYYYYYYYY

	D3DXVECTOR3 debugPosData(-m_DTPos.x, -m_DTPos.y, m_DTPos.z);

	// account for center of sprite
	debugPosData.x += DIRTILESIDE * 0.5;
	debugPosData.y += DIRTILESIDE * 0.5;

	//draw background, plain and simple, not doing any translations or anything
	//but if i was, they would go here as well
	HR(game->Sprite()->Draw(m_DTPSprite, 0, &debugPosData, 0, D3DCOLOR_XRGB(255, 255, 255)));
	HR(game->Sprite()->Flush());

	//be sure to end when done
	HR(game->Sprite()->End());

	// if isEntrance == true, use diff texture

}

void DirTile::incrementDir()
{
	switch(dir)
	{
	case UP:
		dir = RIGHT;
		break;
	case RIGHT:
		dir = DOWN;
		break;
	case DOWN:
		dir = LEFT;
		break;
	case LEFT:
		dir = UP;
		break;
	default:
		dir = UP;
	}
}
